﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class SpouseForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null && int.Parse(Session["UserTypeId"].ToString()) ==1)
            {
                this.Loading();
                if (Session["ClientId"] != null)
                {                   
                    this.Loading(Session["ClientId"].ToString());
                }

            }
            else
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {

                ddlTitleList.DataSource = PersonUtil.GetTitleCollection();
                ddlTitleList.DataBind();
                ddlGender.DataSource = PersonUtil.GetGenderCollection();
                ddlGender.DataBind();
            }

        }

        private void Loading(string client_id)
        {
            if (!Page.IsPostBack)
            {


                Spouse spouse = SpouseServices.GetSpouseByClientId(client_id);
                txbClientId.Text = spouse.ClientId;
                txbClientId.ReadOnly = true;
                txbSpouseId.Text = spouse.SpouseId;
                txbSpouseId.ReadOnly = true;
                ddlTitleList.SelectedValue = spouse.Title;
                txbFullName.Text = spouse.Other_Names;
                txbDob.Text = spouse.DoB.ToShortDateString();
                ddlGender.SelectedValue = spouse.Gender;
                txbAddRes.Text = spouse.AddressRes;
                txbAddOffice.Text = spouse.AddressOff;
                txbTelMobile.Text = spouse.TeleMob;
                txbTelRes.Text = spouse.TeleRes;
                txbTelOffice.Text = spouse.TeleOff;
                txbFax.Text = spouse.Fax;
                txbEmail.Text = spouse.Email;
                txbOccupation.Text = spouse.Occupation;
                txbRemarks.Text = spouse.Remarks;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Spouse spouse = new Spouse();
            spouse.ClientId = txbClientId.Text;
            spouse.SpouseId = txbSpouseId.Text.ToUpper();
            spouse.Title = ddlTitleList.SelectedValue;
            spouse.Other_Names = txbFullName.Text;
            DateTime dob = DateTime.MinValue;
            if (DateTime.TryParse(txbDob.Text, out dob))
            {
                spouse.DoB = dob;
            }
            spouse.Gender = ddlGender.SelectedValue;
            spouse.AddressRes = txbAddRes.Text;
            spouse.AddressOff = txbAddOffice.Text;
            spouse.TeleMob = txbTelMobile.Text;
            spouse.TeleRes = txbTelRes.Text;
            spouse.TeleOff = txbTelOffice.Text;
            spouse.Fax = txbFax.Text;
            spouse.Email = txbEmail.Text;
            spouse.Occupation = txbOccupation.Text;
            spouse.Remarks = txbRemarks.Text;

            if (Session["ClientId"] != null)
            {
                SpouseServices.Update(spouse);
            }
            else
            {
                SpouseServices.Add(spouse);
            }



        }
    }
}
